/**
 * 文件名：CustomerManager.java
 * 全路径：com.xianshiyue.CustomerManager
 */
package cn.sharetraffic.mi;

import java.util.Calendar;

import cn.sharetraffic.mi.etongdai.BaseETDCustomer;
import cn.sharetraffic.mi.etongdai.NinetyDebt;
import cn.sharetraffic.mi.etongdai.SixDebt;
import cn.sharetraffic.mi.xiaomi.BaseMICustomer;
import cn.sharetraffic.mi.xiaomi.Mi5;
import org.apache.log4j.Logger;


import cn.jpush.api.push.model.Message;

/**
 * 功能： <br>
 * 作者：蒋睿立<br> 
 * 时间：2016年6月27日<br>
 * 版本：<br>
 * 
 */
public class CustomerManager extends Thread{

	private Logger log = Logger.getLogger(getClass());
	private boolean loop = true;
	private final BaseMICustomer[] customers = new BaseMICustomer[] {
//			new Hander2Cusomer(),
//			new LedLight(),
//			new Mi5(),
//			 new Fly(),
//			new Bottle()
	};


	public final BaseETDCustomer[] etd = new BaseETDCustomer[]{
			new SixDebt(),
			new NinetyDebt()
	};


	/**
	 * @return the loop
	 */
	public boolean isLoop() {
		return loop;
	}

	@Override
	public void run() {
		Calendar calendar = Calendar.getInstance();
		log.info("服务器主循环开始");
		loop = true;
		while (loop) {
			try{
				calendar.setTimeInMillis(System.currentTimeMillis());
				int hour = calendar.get(Calendar.HOUR_OF_DAY);
				if (hour > 7 && hour < 22) {
					for (int i = 0; i < etd.length; i++) {
						etd[i].shopping();
					}
					sleepNext(1);// 暂停1秒
				}
				else {
					log.info("没到监控时间，休息！");
					sleepNext(60);// 暂停1分钟
				}
			}catch (Exception e){
				log.error(e.getMessage(), e);
			}

		}
		log.info("服务器主循环结束");
		PushApiManager.getInstance().pushMessageToAll(Message.content("服务器结束循环！"));
/*

		for (int i = 0; i < customers.length; i++) {
			customers[i].setOnFindListener(this);
		}
//		for (int i = 0; i < jd.length; i++) {
//			jd[i].setOnFindListener(this);
//		}
//		for (int i = 0; i < etd.length; i++) {
//			etd[i].setOnFindListener(this);
//		}
		while (loop) {
			calendar.setTimeInMillis(System.currentTimeMillis());
			int hour = calendar.get(Calendar.HOUR_OF_DAY);
			if (hour > 7 && hour < 22) {
				for (int i = 0; i < customers.length; i++) {
					customers[i].shopping();
					sleepNext(1);// 暂停1秒
				}
				
//				for (int i = 0; i < jd.length; i++) {
//					jd[i].shopping();
//					sleepNext(1);//暂停1秒
//				}
//
//				for (int i = 0; i < etd.length; i++) {
//					etd[i].shopping();
//					sleepNext(60);//暂停60秒
//				}

			}
			else {
				log.debug("没到监控时间，休息！");
				nextSleep = 60;//60秒
			}
			sleepNext(nextSleep);
		}
		PushApiManager.getInstance().pushMessageToAll(Message.content("服务器结束循环！"));
		*/
	}

	/**
	 * @param loop
	 *            the loop to set
	 */
	public void setLoop(boolean loop) {
		this.loop = loop;
	}


	private void sleepNext(int seconds) {
		try {
			Thread.sleep(seconds * 1000L);
		} catch (InterruptedException e) {
			log.error(e.getMessage(), e);
		}
	}

}
